package com.tdd.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TddUtils {

    public static void main(String[] args) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = toDate("202104" + 30 + "235959999");
        System.out.println("202104" + 30 + "235959999");
        System.out.println(sdf.format(date));
    }

    public static Date toDate(String date) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        if (date.length() < 4) {
            return null;
        } else {
            if (date.length() >= 6) {
                cal.set(1, Integer.parseInt(date.substring(0, 4)));
                cal.set(2, Integer.parseInt(date.substring(4, 6)) - 1);
            }

            if (date.length() >= 8) {
                cal.set(5, Integer.parseInt(date.substring(6, 8)));
            }

            if (date.length() >= 10) {
                cal.set(11, Integer.parseInt(date.substring(8, 10)));
            }

            if (date.length() >= 12) {
                cal.set(12, Integer.parseInt(date.substring(10, 12)));
            }

            if (date.length() >= 14) {
                cal.set(13, Integer.parseInt(date.substring(12, 14)));
            }

            if (date.length() >= 17) {
                cal.set(14, 0);
            }

            return cal.getTime();
        }
    }
}
